<?php

namespace app\socket\controller;
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */

use think\worker\Server;
use Workerman\Lib\Timer;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Message extends Server
{
    protected $host = '0.0.0.0';
    protected $port = 3314;
    protected $protocol = 'websocket';
    protected $option = [
        'count'   => 4, //设置进程数，默认为4，根据自己的需要和服务器配置合理设置，一般设置进程数为CPU核数的1倍-3倍
        'name'    => 'think2',
//        'transport' => 'ssl',   //设置transport开启ssl，启用wss://
    ];
    protected $context = [
//        'ssl' => array(
//            // 请使用绝对路径
//            'local_cert'                 => __DIR__ . '/../../../fucity.cn.pem', // 也可以是crt文件
//            'local_pk'                   => __DIR__ . '/../../../fucity.cn.key',
//            'verify_peer'                => false,
//            // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
//        ),
    ];
    /**
     * 当客户端连接时触发
     */
    public static function onConnect($connection)
    {
        // 向当前client_id发送数据
    }

   /**
    * 当客户端发来消息时触发
    */
   public static function onMessage($connection, $message): void
   {
        // 向发送消息者回复消息
       $connection->uid = $message;
//       cache()->del('message_'.$connection->uid);
   }
   
   /**
    * 当用户断开连接时触发
    */
   public static function onClose($connection): void
   {
       //  向发送消息者回复消息 【如果断线，自动修改状态为0,清空绑定字段】

   }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker): void
    {
        //心跳
        Timer::add(1, function() use ($worker) {
            $messageData = [];
            while ($message = cache()->lpop(env('REDIS_PROJECT').'message_push')){
                $message = json_decode($message,true);
                $message['img'] = match ($message['type']){
                    3,4,5,7,8,9,10 => '/icon_task/202411/07_1730960087672c5ad715646.png',//测评任务消息
                    6,11, 1, 2, 14, 15, 16,23 => '/icon_task/202411/07_1730960430672c5c2ee2c98.png',//预警报告消息-系统通知-公告-危机干预
                    17 => '/icon_task/202411/07_1730944047672c1c2fd624c.png',//预约咨询消息
                    22 => '/icon_task/202411/07_1730941588672c1294c2e1e.png',//物联网消息
                };
                $messageData[$message['uid']][] = $message;
            }
            if (count($messageData)){
                foreach ($worker->connections as $connection) {
                    if (!empty($messageData[$connection->uid??0])) {
                        foreach ($messageData[$connection->uid??0] as $message) {
                            $connection->send(json_encode($message, true));
                        }
                    }
                }
            }
        });
    }
}
